package HIR.graph;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.*;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.GradientPaintTransformer;
import org.jfree.ui.StandardGradientPaintTransformer;


class pie  extends JPanel implements ChangeListener
 {
	DefaultValueDataset valuedataset;

JPanel res = null;

public  int value;
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		valuedataset.setValue(new Integer(this.value));
	}
	
	public pie(){
		super(new BorderLayout());
		DefaultValueDataset dataset = new DefaultValueDataset(10D);
		JFreeChart jfreechart = this.getJPanel();
		
		
		
		
		
		
		DialPlot dialplot = (DialPlot)jfreechart.getPlot();
		
		StandardDialRange standarddialrange = new StandardDialRange(40D, 60D, Color.red);
		standarddialrange.setInnerRadius(0.52000000000000002D);
		standarddialrange.setOuterRadius(0.55000000000000004D);
		dialplot.addLayer(standarddialrange);
		StandardDialRange standarddialrange1 = new StandardDialRange(10D, 40D, Color.orange);
		standarddialrange1.setInnerRadius(0.52000000000000002D);
		standarddialrange1.setOuterRadius(0.55000000000000004D);
		dialplot.addLayer(standarddialrange1);
		StandardDialRange standarddialrange2 = new StandardDialRange(-40D, 10D, Color.green);
		standarddialrange2.setInnerRadius(0.52000000000000002D);
		standarddialrange2.setOuterRadius(0.55000000000000004D);
		dialplot.addLayer(standarddialrange2);
		GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220));
		DialBackground dialbackground = new DialBackground(gradientpaint);
		dialbackground.setGradientPaintTransformer((GradientPaintTransformer) new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
		dialplot.setBackground(dialbackground);
		dialplot.removePointer(0);
		dialplot.addPointer(new org.jfree.chart.plot.dial.DialPointer.Pointer());

		
		
		
		
		ChartPanel chartpanel = new ChartPanel(jfreechart);
		
		
		
		chartpanel.setPreferredSize(new Dimension(200, 200));
		add(chartpanel);
		
		
	}
	
	
	JFreeChart getJPanel(){
		DialPlot dialplot = new DialPlot();
		valuedataset = new DefaultValueDataset(10D);
		dialplot.setDataset(valuedataset);
		dialplot.setDialFrame(new StandardDialFrame());
		dialplot.setBackground(new DialBackground());
		
		
		DialTextAnnotation dialtextannotation = new DialTextAnnotation("Memory");
		dialtextannotation.setFont(new Font("Dialog", 1, 14));
		dialtextannotation.setRadius(0.69999999999999996D);
		dialplot.addLayer(dialtextannotation);
		DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
		dialplot.addLayer(dialvalueindicator);
		StandardDialScale standarddialscale = new StandardDialScale(0D, 100D, -120D, -300D, 10D, 4);
		standarddialscale.setMajorTickIncrement(10);
		standarddialscale.setMinorTickCount(1);
		standarddialscale.setTickRadius(0.88D);
		standarddialscale.setTickLabelOffset(0.14999999999999999D);
		standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));
		dialplot.addScale(0, standarddialscale);
		dialplot.addPointer(new org.jfree.chart.plot.dial.DialPointer.Pin());
		DialCap dialcap = new DialCap();
		dialplot.setCap(dialcap);
		return new JFreeChart("Memory", dialplot);
	}
	
	
}
public class sysinfoFrame extends JFrame implements   Runnable{
	
	
	private pie pie;
	
	

	public void run() {
		ChangeEvent Event = new ChangeEvent(this);
		while (this.isVisible()){
		 
			Runtime.getRuntime().gc();
			long free =  Runtime.getRuntime().freeMemory();
			long total = Runtime.getRuntime().totalMemory();
	
	       this.pie.value = (int ) ( (total - free)  *100 / total);
	       
	       this.pie.stateChanged(Event);
	       try {
	    	  if (this.pie.value > 95 ) {
	    		  JOptionPane.showMessageDialog(this,
	    				    "Not enough memory , Increase JVM memory . max is " + total + " Mb",
	    				    "Inane error",
	    				    JOptionPane.ERROR_MESSAGE);
	    	  }
	    	   /*System.out.println("Total Memory "+Runtime.getRuntime().totalMemory()/1024);
	 		  
	 		  System.out.println("Total Memory "+Runtime.getRuntime().maxMemory()/1024);
	 	      System.out.println("Free Memory "+Runtime.getRuntime().freeMemory()/1024);*/
	    	   
	 	     Graphics2D screenGraphics =(Graphics2D) this.getGraphics(); 
	 		this.paint(screenGraphics);
	    	   
			Thread.sleep(1000);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		// TODO Auto-generated method stub
		
	}
	public JPanel createPanel()
	{
		JPanel jpanel = new JPanel(new GridLayout(1, 1));
		//jpanel.add(new DemoPanelA());
		//jpanel.add(new DemoPanelB());
		this.pie = new pie();
		jpanel.add(pie);
		
		return jpanel;
	}
	
	public sysinfoFrame(){

	       
	       
	       
			super();
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			setContentPane(createPanel());
			//setContentPane(createDemoPanel());
			Thread info = new Thread(this);
		

		       
		       this.pack();
		       this.setVisible(true);
		   	info.start();
		
	}
	
	

}
